#include <iostream>
#include <cmath>
#include "Account.h"
using namespace std;

double Account::total = 0;
Account::Account(const Date& date, const string& idNum, double rateNum) :id(idNum), balance(0), rate(rateNum), lastDate(date), accumulation(0)
{
	date.show();
	cout << "\t#" << id << " is created!" << endl;
}


void Account::record(const Date& date, double amount, const string& desc)
{
	accumulation = accumulate(date);
	lastDate = date;
	amount = floor(amount * 100 + 0.5) / 100;
	balance += amount;
	total += amount;
	date.show();
	cout <<  "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
}

void Account::error(string&& msg) const
{
	cout << "Error(#" << id << ")" << msg << endl;
}

void Account::show() const
{
	cout << "Now\t#" << id << "\tBalance:" << balance << endl;
}


void Account::deposit(const Date& date, double amount, const string& desc)
{
	record(date, amount,desc);
}

void Account::withdraw(const Date& date, double amount, const string& desc)
{
	if (amount > getBalance())
	{
		error("not enough money!");
	}
	else
	{
		record(date, -amount,desc);
	}
}


void Account::settle(const Date& date)
{
	double interest = accumulate(date) * rate / date.distance(Date(date.getYear()-1,1,1));
	if (interest != 0)
	{
		record(date, interest,"interest");
	}
	accumulation = 0;
}

